package common;

import java.io.*;
import java.util.*;


public class Statistic {
	private HashMap<Character, Integer> count = new HashMap<Character, Integer>();
	private HashMap<Character, Integer> cumCount = new HashMap<Character, Integer>();
	private ArrayList<Character> lettersSet;
	private int totalCount = 0;
	
	
	public Statistic(String input){
		createStatistic(input);
	}
	
	
	public Statistic(FileReader file) throws IOException{
		BufferedReader br = new BufferedReader(file);
		
		try{
			
			StringBuilder sb = new StringBuilder();
			String line = br.readLine();
			
			while(line != null){
				sb.append(line);
				sb.append("\n");
				line = br.readLine();
			}
			
			createStatistic(br.toString());
		}finally {
			br.close();
		}
	}
	
	
	
	private void createStatistic(String input){
		char[] arr = input.toCharArray();
		this.totalCount += (arr.length);
		
		for(Character ch : arr){
			if(count.containsKey(ch))
				this.count.put(ch, count.get(ch)+1);
			else
				this.count.put(ch, 1);
		}
		
		lettersSet = new ArrayList<Character> (count.keySet());
		int cum=0;
		
		for(Character ch : lettersSet){
			cum += this.count.get(ch);
			this.cumCount.put(ch, cum);
		}
	}


	public int getTotalCount() {
		return totalCount;
	}
	
	public int getLetterCount(char letter){
		return count.get(letter);
	}
	
	public int getLetterCumCount(char letter){
		return cumCount.get(letter);
	}
	
	public int getPreviousLetterCumCount(char letter){
		int pos = lettersSet.indexOf(letter);
		if(pos == 0)
			return 0;
		else
			return cumCount.get(lettersSet.get(pos-1));
	}

}
